package com.salelogistics.basic.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.bean.BeanUtil;
import com.salelogistics.basic.domain.BasLot;
import com.salelogistics.basic.domain.BasMaterialType;
import com.salelogistics.basic.domain.vo.BasLotVo;
import com.salelogistics.basic.domain.vo.BasMaterialVo;
import com.salelogistics.basic.domain.vo.TreeSelect;
import com.salelogistics.basic.service.IBasMaterialTypeService;
import com.salelogistics.basic.utils.CheckParameterUtil;
import com.salelogistics.common.core.utils.StringUtils;
import com.salelogistics.common.security.utils.SecurityUtils;
import com.salelogistics.system.api.domain.SysDept;
import com.salelogistics.system.api.domain.SysUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.salelogistics.common.log.annotation.Log;
import com.salelogistics.common.log.enums.BusinessType;
import com.salelogistics.common.security.annotation.RequiresPermissions;
import com.salelogistics.basic.domain.BasMaterial;
import com.salelogistics.basic.service.IBasMaterialService;
import com.salelogistics.common.core.web.controller.BaseController;
import com.salelogistics.common.core.web.domain.AjaxResult;
import com.salelogistics.common.core.utils.poi.ExcelUtil;
import com.salelogistics.common.core.web.page.TableDataInfo;

/**
 * 材料Controller
 *
 * @author ruoyi
 * @date 2023-07-10
 */
@RestController
@RequestMapping("material")
public class BasMaterialController extends BaseController {
    //日志打印类
    private static final Logger log = LoggerFactory.getLogger(BasSupplierController.class);

    //数据校验工具
    CheckParameterUtil checkParameterUtil = new CheckParameterUtil();

    @Autowired
    private IBasMaterialService basMaterialService;

    @Autowired
    private IBasMaterialTypeService basMaterialTypeService;

    /**
     * 查询材料列表
     */
    @RequiresPermissions("basic:material:listVo")
    @GetMapping("/listVo")
    public TableDataInfo listVo(BasMaterial basMaterial) {
        List<BasMaterialVo> listVo = new ArrayList<>();
        startPage();
        List<BasMaterial> list = basMaterialService.selectBasMaterialList(basMaterial);
        for (int i = 0; i < list.size(); i++) {
            BasMaterialVo basMaterialVo = BeanUtil.toBean(list.get(i), BasMaterialVo.class);
            listVo.add(basMaterialVo);
        }
        return getDataTable(listVo);
    }

    /**
     * 查询材料列表
     */
    @RequiresPermissions("basic:material:list")
    @GetMapping("/list")
    public TableDataInfo list(BasMaterial basMaterial) {
        System.out.println(basMaterial);
        Long typeId = basMaterial.getmaterialTypeId();
        List<BasMaterialType> types = basMaterialTypeService.selectTypeParent(typeId);
        startPage();
        if (types.size() > 0 && typeId != 1) {
            List<BasMaterial> list = basMaterialService.selectBasMaterialParentList(typeId);
            System.out.println(list);
            return getDataTable(list);
        } else {
            List<BasMaterial> list = basMaterialService.selectBasMaterialList(basMaterial);
            return getDataTable(list);
        }
    }

    /**
     * 导出材料列表
     */
    @RequiresPermissions("basic:material:export")
    @Log(title = "材料", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BasMaterial basMaterial) {
        List<BasMaterial> list = basMaterialService.selectBasMaterialList(basMaterial);
        ExcelUtil<BasMaterial> util = new ExcelUtil<BasMaterial>(BasMaterial.class);
        util.exportExcel(response, list, "材料数据");
    }

    /**
     * 获取材料详细信息
     */
    @RequiresPermissions("basic:material:query")
    @GetMapping(value = "/{materialId}")
    public AjaxResult getInfo(@PathVariable("materialId") Long materialId) {
        return success(basMaterialService.selectBasMaterialByMaterialId(materialId));
    }

    /**
     * 新增材料
     */
    @RequiresPermissions("basic:material:add")
    @Log(title = "材料", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BasMaterial basMaterial) {
        if (checkParameterUtil.checkParameter(basMaterial, "basMaterial")) {

            basMaterial.setCreateBy(SecurityUtils.getUsername());
            basMaterial.setEditedBy(SecurityUtils.getUsername());
            return toAjax(basMaterialService.insertBasMaterial(basMaterial));
        } else {
            return error("参数异常");
        }
    }

    /**
     * 修改材料
     */
    @RequiresPermissions("basic:material:edit")
    @Log(title = "材料", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BasMaterial basMaterial) {
        System.out.println(basMaterial + "修改信息");
        if (checkParameterUtil.checkParameter(basMaterial, "basMaterial")) {
            // 创建一个Date对象，表示当前时间
            Date date = new Date();
            // 创建一个SimpleDateFormat对象，指定时间格式
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            // 使用SimpleDateFormat对象格式化Date对象，获取当前时间的字符串表示
            String currentTime = sdf.format(date);
            basMaterial.setEditedBy(SecurityUtils.getUsername());
            basMaterial.setEditedTime(currentTime);
            return toAjax(basMaterialService.updateBasMaterial(basMaterial));
        } else {
            return error("参数异常");
        }
    }

    /**
     * 状态修改
     */
    @RequiresPermissions("basic:material:edit")
    @Log(title = "材料", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody BasMaterial basMaterial) {
        // 创建一个Date对象，表示当前时间
        Date date = new Date();
        // 创建一个SimpleDateFormat对象，指定时间格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 使用SimpleDateFormat对象格式化Date对象，获取当前时间的字符串表示
        String currentTime = sdf.format(date);
        basMaterial.setEditedBy(SecurityUtils.getUsername());
        basMaterial.setEditedTime(currentTime);
        return toAjax(basMaterialService.updateMaterialStatus(basMaterial));
    }

    /**
     * 删除材料
     */
    @RequiresPermissions("basic:material:remove")
    @Log(title = "材料", businessType = BusinessType.DELETE)
    @DeleteMapping("/{materialIds}")
    public AjaxResult remove(@PathVariable Long[] materialIds) {
        return toAjax(basMaterialService.deleteBasMaterialByMaterialIds(materialIds));
    }

    /**
     * 获取材料类型树列表
     */
    @RequiresPermissions("system:type:list")
    @GetMapping("/typeTree")
    public AjaxResult typeTree(BasMaterialType basMaterialType) {
        List<TreeSelect> types = basMaterialTypeService.selectTypeTreeList(basMaterialType);
        return success(types);
    }

    /**
     * 获取材料类型父元素编号
     */
    @RequiresPermissions("system:type:list")
    @GetMapping("/getTypeParentId")
    public AjaxResult getTypeParentId(Long materialTypeId) {
        BasMaterialType type = basMaterialTypeService.selectBasMaterialTypeByMaterialTypeId(materialTypeId);

        return success(type);
    }


}
